const mongo = require("./mongoose")
const c = require('../config');
c.timeZoneSetting()
mongo.mgsConect(c)
setTimeout(() => {
  // findAllUser()
  // createTestUser()
  // bindBuddy()
  // getBuddyByOpenId('wx123456789')
  // getMarksDateByOid(wx0)
  // getAllusers()
  // setUser('wx12345678910','2022-12-26 20:20:27')
  getMarks(wx0)
}, 1000);

const wx0 = 'of4Dn5fDiWP9XGFvQYSA3K_G7Qeg'

// 查询用户列表
async function findAllUser() {
  console.log('findAllUser go')
  let r = await mongo.find('user', {}, {}, {}, {})
  for (let i in r) {
    console.log(`name:${r[i].name}`)
  }
}
// 插入测试用户数据
async function createTestUser() {
  let arr = []
  for (let i = 1; i <= 10; i++) {
    let o = {}
    o.openId = 'wx123456789' + i
    o.name = '测试用户' + i
    o.createTime = new Date().zoneDate()
    arr.push(o)
  }
  console.log('createTestUser go')
  let r = await mongo.insertMany('user', arr)
  for (let i in r) {
    console.log(r[i])
  }
}
// 插入绑定朋友测试数据
async function bindBuddy() {
  let arr = []
  for (let i = 1; i <= 9; i++) {
    // wx0与除了wx10以外所有人绑定为朋友
    arr.push({ openId: wx0, buddyOpenId: 'wx123456789' + i, createTime: new Date().zoneDate() })
    arr.push({ openId: 'wx123456789' + i, buddyOpenId: wx0, createTime: new Date().zoneDate() })
    // wx10与wx9-wx5绑定为朋友
    if (i >= 5) {
      arr.push({ openId: 'wx12345678910', buddyOpenId: 'wx123456789' + i, createTime: new Date().zoneDate() })
      arr.push({ openId: 'wx123456789' + i, buddyOpenId: 'wx12345678910', createTime: new Date().zoneDate() })
    }
    // wx1与wx奇数绑定
    if (i > 1 && i % 2 == 1) {
      arr.push({ openId: 'wx1234567891', buddyOpenId: 'wx123456789' + i, createTime: new Date().zoneDate() })
      arr.push({ openId: 'wx123456789' + i, buddyOpenId: 'wx1234567891', createTime: new Date().zoneDate() })
    }
    // wx2与偶数绑定
    if (i > 2 && i % 2 == 0) {
      arr.push({ openId: 'wx1234567892', buddyOpenId: 'wx123456789' + i, createTime: new Date().zoneDate() })
      arr.push({ openId: 'wx123456789' + i, buddyOpenId: 'wx1234567892', createTime: new Date().zoneDate() })
    }
  }

  console.log('bindBuddy go')
  console.log(arr)
  let r = await mongo.insertMany('buddy', arr)
  for (let i in r) {
    console.log(r[i])
  }
}

// 按openId查询朋友
async function getBuddyByOpenId(oid) {
  let r = await mongo.getBuddysByOid(oid)
  // console.log(r)
  console.log('openId:' + oid)
  for (let i in r) {
    console.log(r[i].buddyOpenId + '[' + r[i].buddyName + ']')
  }
}

// 按openid查询所有记录日期
async function getMarksDateByOid(oid) {
  let r = await mongo.getMarksDateByOid(oid)
  console.log(r)
}


// 查询所有用户
async function getAllusers() {
  let sort = { 'createTime': 1 }
  let paging = {}
  // paging.index = data.indexPage || 1
  // paging.size = data.pageSize || 0
  let r = await mongo.find('user', {}, {}, sort, paging)
  // console.log(r)
  for (let i in r) {
    console.log('****************' + r[i].openId + '\n' + r[i].name + '\n' + r[i].createTime)
  }
}

async function setUser(oid, newDt) {
  let filter = { openId: oid }
  let updatejson = { createTime: newDt }
  let r = await mongo.updateOne('user', filter, updatejson)
  console.log(r)
}

// 查询打卡点
async function getMarks(oid) {
  let filter = {}
  filter.openId = oid
  filter.markTime = { $gte: '2023-02-10 00:00:00', $lte: '2023-02-10 23:59:59' }
  let field = {}
  field.imgs = 0
  let sort = { markTime: -1 }
  let r = await mongo.find('mark', filter, field, sort, {})
  console.log(r)
}